package com.tg.push;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.alibaba.sdk.android.push.CommonCallback;
import com.tg.appcommon.android.TGLog;

public class AliyunPusher extends BasePusher {

    private static final String TAG = PushManager.TAG + "AliyunPusher";

    @Override
    public void register(Context application, TGCommonCallback callback) {
        TGLog.i(TAG, "register: start...");
        CloudPushService pushService = PushServiceFactory.getCloudPushService();
        pushService.register(application, new CommonCallback() {
            @Override
            public void onSuccess(String response) {
                deviceId = pushService.getDeviceId();
                callback.onSuccess(deviceId);
                TGLog.i(TAG, "register: onSuccess deviceId = " + deviceId);
            }

            @Override
            public void onFailed(String errorCode, String errorMessage) {
                TGLog.i(TAG, "register: onFailed errorCode = " + errorCode);
                TGLog.i(TAG, "register: onFailed errorMessage = " + errorMessage);
                callback.onFailed(errorCode,errorMessage);
                setErrorMsg(errorCode,errorMessage);
            }
        });
    }

    @Override
    public String getPushChannel() {
        return "aliyun";
    }

    @Override
    public String getDeviceId() {
        deviceId =  PushServiceFactory.getCloudPushService().getDeviceId();
        if (TextUtils.isEmpty(deviceId))return super.getDeviceId();
        return deviceId;
    }

    @Override
    public void init(Context context) {
        super.init(context);
        AliYunPushFixer.modifyAliYunInfo(context);
        PushServiceFactory.init(context);
    }

}
